package dbTools.pojo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import projectsupporter.common.control.Path;

import dbTools.DBToolEngine;
import dbTools.pojo.structure.BeanSubject;
import dbTools.pojo.structure.property.BeanProperty;
import dbTools.pojo.structure.writer.writers.SimpleWriter;
import dbTools.tableInfo.field.Field;
import dbTools.tableInfo.field.fieldInfo.type.types.TypeReference;
import dbTools.tableInfo.info.TableInfo;

public class JavaBeanCreator {

	public static Map<String,String> createPOJOs(Map<String,TableInfo> tableInfos,Path mainPath,boolean folderById)
	{
		Map<String,String> pojoClassMap=new LinkedHashMap<String, String>();
		SimpleWriter writer=new SimpleWriter();
		for(Map.Entry<String,TableInfo> entry : tableInfos.entrySet())
		{
			//mainFileName
			TableInfo tableInfo= entry.getValue();
			String fileName = tableInfo.getKey();
			
			BeanSubject pojo=new BeanSubject(fileName);
			
			String path = mainPath.getAbsolutePath();
			String packStr = mainPath.getPackagePath();
			if (folderById) {
				path += "\\" + tableInfo.getTableId();
				packStr+= "." + tableInfo.getTableId();
			}
			pojo.setPackageStr(packStr);
			pojo.setPath(path);
			
			List<BeanProperty> propertys=new ArrayList<BeanProperty>();
			List<String> importList=new ArrayList<String>();
			
			//keys
			setKeys(tableInfo, propertys , pojo ,importList,mainPath,folderById);
			//
			setPropeters( tableInfo, propertys ,importList, mainPath, folderById);
			
			pojo.setPropertys(propertys);
			pojo.setImportItems(importList);
			pojo.setWriter(writer);
			pojo.createBean();
			
			pojoClassMap.put(fileName, packStr+"."+fileName);
			if(pojo.getId()!=null)
			{
				pojoClassMap.put(fileName+"Id", packStr+"."+fileName+"Id");
			}
		}
		return pojoClassMap;
	}
	
	private static void setKeys(TableInfo tableInfo, List<BeanProperty> propertys,BeanSubject pojo,List<String> importList
								,Path mainPath,boolean folderById)
	{
		if(tableInfo.getKeyFields().size()==1)
		 {
			Object[] keyValuePairs = tableInfo.getKeyFields().entrySet().toArray();
			Map.Entry<String, Field> entry = (Map.Entry<String, Field>)keyValuePairs[0];
			Field keyField=entry.getValue();
			BeanProperty property= getProperty(keyField,importList,mainPath,folderById);
			propertys.add(property);
		 }
		 else
		 {
			 BeanSubject id=new BeanSubject(pojo.getFileName()+"Id");
			 id.setId(true);
			 id.setPackageStr(pojo.getPackageStr());
			 id.setPath(pojo.getPath());
			 List<BeanProperty> idPropertys=new ArrayList<BeanProperty>();
			 List<String> idImportList=new ArrayList<String>();
			 for(Map.Entry<String,Field> fieldEntry : tableInfo.getKeyFields().entrySet())
			 {
				 Field field=fieldEntry.getValue();
				 BeanProperty property= getProperty(field,idImportList,mainPath,folderById);
				 idPropertys.add(property);
			 } 
			 id.setPropertys(idPropertys);
			 id.setImportItems(idImportList);
			 pojo.setId(id);
			 
			 //add keyPropertys
			 BeanProperty keyProperty = new BeanProperty();
			 keyProperty.setGet(true);
			 keyProperty.setSet(true);
			 keyProperty.setName("id");
			 keyProperty.setNote("id");
			 keyProperty.setTypeStr(id.getFileName());
			 
			 propertys.add(keyProperty);
			 
			 importList.add(id.getPackageStr()+"."+id.getFileName());
		 }
	}

	private static void setPropeters(TableInfo tableInfo,
			List<BeanProperty> propertys ,List<String> importList
			,Path mainPath,boolean folderById) {
		for(Map.Entry<String,Field> fieldEntry : tableInfo.getAllFields().entrySet())
		 {
			 Field field=fieldEntry.getValue();

			 if(field.isKey())
			 {
				 continue;
			 }
			 
			 BeanProperty property= getProperty(field,importList,mainPath,folderById);
			 propertys.add(property);
		 }
	}
	
	private static BeanProperty getProperty(Field field,List<String> importList,Path mainPath,boolean folderById)
	{
		 BeanProperty property= new BeanProperty();
		 property.setName(field.getFieldName());
		 property.setGet(true);
		 property.setSet(true);
		 property.setNote(field.getItemName());
		 
		 TypeReference ref = DBToolEngine.getTypeReference_REFOnly(field);
		 if(ref==null)
		 {
			 String type=field.getDataTypeString();
			 if(type.lastIndexOf('.')>0)
			 {
				 type=type.substring(type.lastIndexOf('.')+1, type.length());
			 }
			 property.setTypeStr(type);
		 }
		 else
		 {
			 TableInfo refTable=DBToolEngine.findTable(ref.getReferenceId());
			 String fileName = refTable.getTableId().substring(0,1).toUpperCase()+ refTable.getTableId().substring(1);
			 
			 property.setTypeStr(fileName);
			 
			 String packStr = mainPath.getPackagePath();
				if (folderById) {
					packStr+= "." + refTable.getTableId();
				}
			 importList.add(packStr+"."+fileName);
		 }

		 if(field.getDataTypeString().equals(Field.TYPE_DATE))
		 {
			 if(importList.contains(field.getDataTypeString())==false)
			 {
				 importList.add(field.getDataTypeString());
			 }
		 }
		 
		 return property;
	}
}
